package com.xiashitech.agent.service.extloadproxy;

import com.xiashitech.agent.cache.classloader.ClassLoaderCache;
import com.xiashitech.agent.config.StartConfig;
import com.xiashitech.agent.db.entity.DataWaterMarkConfig;

public class AuthServiceProxy {

    private static String className = AuthServiceProxy.class.getName().substring(0, AuthServiceProxy.class.getName().length() - 5).replace("extloadproxy","extload");

//    /**
//     * 代理AuthQueryService类的queryPermissions方法
//     * @param userId
//     * @param schemaName
//     * @param tableName
//     * @param operate
//     * @return
//     */
//    public static List<UserPermission> queryPermissions(String userId, String schemaName, String tableName, String operate) {
//        try {
//            Class authQueryServiceClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
//            Object authQueryServiceInstance = authQueryServiceClass.getMethod("getInstance").invoke(null);
//            return (List<UserPermission>)authQueryServiceInstance.getClass().getMethod("queryPermissions", String.class, String.class, String.class, String.class).invoke(authQueryServiceInstance,userId,schemaName,tableName,operate);
//        } catch (Throwable e) {
//            StartConfig.throwableStackPrinter(e);
//        }
//        return null;
//    }

//    /**
//     * 代理AuthQueryService类的queryDesensitizeInfo方法
//     * @param schemaName
//     * @param tableName
//     * @param colName
//     * @return
//     */
//    public static List<DesensitiseRule> queryDesensitizeInfo(String schemaName, String tableName, String colName) {
//        try {
//            Class authQueryServiceClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
//            Object authQueryServiceInstance = authQueryServiceClass.getMethod("getInstance").invoke(null);
//            return (List<DesensitiseRule>)authQueryServiceInstance.getClass().getMethod("queryDesensitizeInfo", String.class, String.class, String.class).invoke(authQueryServiceInstance,schemaName,tableName,colName);
//        } catch (Throwable e) {
//            StartConfig.throwableStackPrinter(e);
//        }
//        return null;
//    }

    public static void initAuthService() {
        try {
            Class authServiceClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            Object authServiceInstance = authServiceClass.getMethod("getInstance").invoke(null);
            authServiceClass.getMethod("init").invoke(authServiceInstance);
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
    }

    public static void onMessage(String msg) {
        try {
            Class authServiceClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            Object authServiceInstance = authServiceClass.getMethod("getInstance").invoke(null);
            authServiceClass.getMethod("onMessage", String.class).invoke(authServiceInstance, msg);
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
    }

    public static void loadDataWaterMarkConfigCache(DataWaterMarkConfig config) {
        try {
            Class authServiceClass = ClassLoaderCache.getLaunchClassLoader().loadClass(className);
            Object authServiceInstance = authServiceClass.getMethod("getInstance").invoke(null);
            authServiceClass.getMethod("loadDataWaterMarkConfigCache", DataWaterMarkConfig.class).invoke(authServiceInstance, config);
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
    }
}
